package com.boarsoft.validate.jsonschema.networknt;

import com.boarsoft.validate.jsonschema.JsonSchemaCache;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.ValidationMessage;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class Validator {


    public static void validate(String schemaId, Map<String,Object> obj){
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.valueToTree(obj);
        Set<ValidationMessage> errors = JsonSchemaCache.getSchema(schemaId).validate(node);
        if (!errors.isEmpty()) {
            throw new RuntimeException(errors.iterator().next().getMessage());
        }
    }
    public static void validate(String schemaId, Collection<Object> obj){
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.valueToTree(obj);
        Set<ValidationMessage> errors = JsonSchemaCache.getSchema(schemaId).validate(node);
        if (!errors.isEmpty()) {
            throw new RuntimeException(errors.iterator().next().getMessage());
        }
    }
}
